#!/usr/bin/env python
    ##########################################################################
    ##(c) 2008 Bastian Damman                                               ##
    ##                                                                      ##
    ##This file is part of Kaboom-O-rama.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is free software: you can redistribute it and/or modify ##
    ##it under the terms of the GNU General Public License as published by  ##
    ##the Free Software Foundation, either version 3 of the License, or     ##
    ##(at your option) any later version.                                   ##
    ##                                                                      ##
    ##Kaboom-O-rama is distributed in the hope that it will be useful,      ##
    ##but WITHOUT ANY WARRANTY; without even the implied warranty of        ##
    ##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         ##
    ##GNU General Public License for more details.                          ##
    ##                                                                      ##
    ##You should have received a copy of the GNU General Public License     ##
    ##along with Kaboom-O-rama.  If not, see <http://www.gnu.org/licenses/>.##
    ##########################################################################

screenWidth = 800
screenHeight = 600

import pygame
from pygame import *
from Grid import Grid
from Tool import Tool
from Camera import Camera
from Object import *
from Level import Level
from Form.DropDown import DropDown
from Properties import Properties
from Terminal import Terminal

class Main:
    def __init__(self, fps):
        pygame.init()
        pygame.font.init()
        self.font_fps = font.SysFont(font.get_default_font(), 16)
        self.fps = 0
        self.initGame(fps)
        self.load()
        
    def initGame(self, fps):
        self.screen = display.set_mode((screenWidth, screenHeight), SRCALPHA) #init screen + set alpha enabled
        #mouse.set_visible(False)

        self.camera = Camera(self.screen)
        
        self.properties = Properties()      
        self.grid = Grid(self.properties.grid_width, self.properties.grid_height, self.properties.grid_celsize)
        self.properties.set_grid(self.grid)
        
        self.terminal = Terminal(self, fps)
        
    def load(self):        
        self.ic = ImageContainer()
        self.ic.spr_tool_ground = []
        self.ic.spr_tool_ground.append(self.ic.load("Data/Textures/Tools/ground_sc1.png"))
        
        self.tool = Tool(self)       
        self.level = Level()
    
    def save(self):
        self.level.save()
    
    def update(self, clock):
        events = event.get()
        
        self.terminal.update(events, self.properties)
        
        self.camera.mouse_input(events)
        
        mouse_left_click = False
        for e in events:
            if e.type == QUIT:
                return 1
            elif e.type == KEYDOWN:
                if e.key == K_ESCAPE:
                    self.save()
                    return 1
            elif e.type == MOUSEBUTTONDOWN:
                if e.button == 1:
                    mouse_left_click = True
        
        self.tool.update(self.grid, events)
        
        self.fps = clock.get_fps()
        return 0
    
    def draw(self):
        self.screen.fill((129,182,255)) #clears the screen      
        self.grid.draw(self.camera)
        
        self.tool.draw(self.camera)
        
        t = self.font_fps.render("fps: " + str(self.fps), True, (255, 255, 255))
        self.screen.blit(t, (16, 16))
        
        self.terminal.draw(self.screen)
        
        display.update()

class ImageContainer:
    def __init__(self):
        pass
    def load(self, path, alpha = False):
        img = image.load(path)
        if alpha:
            img.convert_alpha()
        else:
            img.convert()
        return img